package com.woniu.car.feigncontroller;

import com.alibaba.fastjson.JSONObject;
import com.woniu.car.annotation.TelAndPwdCheck;
import com.woniu.car.common.ResponseEntity;
import com.woniu.car.domain.TBackstageAccount;
import com.woniu.car.domain.TRoleUser;
import com.woniu.car.domain.TUrl;
import com.woniu.car.dto.ShopRegisterDto;
import com.woniu.car.exception.AccountException;
import com.woniu.car.param.QueryRolePermissionParam;
import com.woniu.car.param.ShopRegisterParam;
import com.woniu.car.param.UrlRolesJson;
import com.woniu.car.service.TBackstageAccountService;
import com.woniu.car.service.TRoleUserService;
import com.woniu.car.service.TShopAccountService;
import com.woniu.car.service.TUrlService;
import com.woniu.car.util.Code;
import com.woniu.car.util.IsSuper;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName FeignController
 * @Description
 * @Author 王维鑫
 * @Version
 **/
@RestController
@RequestMapping("/FeignController")
public class FeignController {
    @Resource
    private TRoleUserService tRoleUserService;
    @Resource
    private TUrlService tUrlService;
    @Resource
    private TShopAccountService tShopAccountService;
    @Resource
    private TBackstageAccountService tBackstageAccountService;
    /**
     *@Author 王维鑫
     *@Description 根据用户ID和请求地址查询权限
     *@Param
     *@return
     **/
    @ApiOperation("根据用户ID和请求地址查询权限")
    @PostMapping("queryRolePermission")
    public ResponseEntity queryRolePermission(@RequestBody QueryRolePermissionParam param){
        Integer id = param.getUserId();
        //通过用户ID找到用户,判断是否时超管
        TBackstageAccount tBackstageAccount = new TBackstageAccount();
        tBackstageAccount.setBackstageId(id);
        List<TBackstageAccount> tBackstageAccounts = tBackstageAccountService.queryBackstageAccount(tBackstageAccount);
        if(tBackstageAccounts.size()==0){
            throw new AccountException("非法用户，停止访问");
        }else {
            System.out.println("进入1");
            TBackstageAccount tBackstageAccount1 = tBackstageAccounts.get(0);
            Integer isSuper = tBackstageAccount1.getIsSuper();
            if (isSuper.equals(IsSuper.NORMAL)) {
                System.out.println("进入2");
            String uri = param.getUri();
                System.out.println(uri);
                //通过ID找到角色
            TRoleUser tRoleUser = new TRoleUser();
            tRoleUser.setUserId(id);
            List<TRoleUser> tRoleUsers = tRoleUserService.queryRoleUser(tRoleUser);
            //将用户所拥有的所有角色ID统计
            ArrayList<Integer> integers = new ArrayList<>();
            for (TRoleUser roleUser : tRoleUsers) {
                integers.add(roleUser.getRoleId());
            }
            //根据当前接口地址查到该方法对应的所有角色ID集合
            TUrl tUrl = new TUrl();
            tUrl.setUrl(uri);
            TUrl tUrl1 = tUrlService.queryUrl(tUrl);
            String roleIds = tUrl1.getRoleIds();
            if (null==roleIds||roleIds.equals("")) {
                System.out.println("进入3");
                return new ResponseEntity().setCode(Code.ERROR).setMsg("没有权限");
            } else {
                boolean bool = false;
                ArrayList<Integer> integers1 = new ArrayList<>();
                List<UrlRolesJson> urlRolesJsons = JSONObject.parseArray(roleIds, UrlRolesJson.class);
                for (UrlRolesJson urlRolesJson : urlRolesJsons) {
                    integers1.add(urlRolesJson.getRoleId());
                }
                for (Integer integer : integers) {
                    for (Integer integer1 : integers1) {
                        if (integer.equals(integer1)) {
                            bool = true;
                            break;
                        }
                    }
                }
                if (bool == true) {
                    return new ResponseEntity().setCode(Code.NORMAL).setMsg("拥有权限");
                } else {
                    return new ResponseEntity().setCode(Code.ERROR).setMsg("没有权限");
                }
            }
        }else {
                return new ResponseEntity().setCode(Code.NORMAL).setMsg("超级管理员");
            }
    }
}


    /**
     *@Author 王维鑫
     *@Description 商铺注册
     *@Param
     *@return
     **/
    @TelAndPwdCheck
    @ApiOperation("商家账号注册接口")
    @PostMapping("shopRegister")
    public ResponseEntity shopRegister(@RequestBody ShopRegisterParam param) throws Exception {
        ShopRegisterDto shopAccountDto = tShopAccountService.registerShopAccount(param);
        return  ResponseEntity.builder(ShopRegisterDto.class).setObject(shopAccountDto).setCode(Code.NORMAL).setMsg("注册成功");
    }
}
